﻿using System;

namespace Program19
{
    class Program
    {
        public delegate int DelegateCompare<T>(T t1, T t2);
        static void Main(string[] args)
        {
            //1.用方法来实现：有一个字符串数组：{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。
            string[] names = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" };
            string namesMax = GetMax<string>(names, Compare2);
            Console.WriteLine("最长的字符串为:{0}", namesMax);

            //2.用方法来实现：请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }。要求：计算结果如果有小数，则显示小数点后两位（四舍五入）。
            int[] a = { 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 };
            double avg = GetAvg(a);
            avg = Convert.ToDouble(avg.ToString("0.00"));
            Console.WriteLine("数组的平均值为：{0}", avg);
           

            //3.请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。
            int[] nums = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
            MaoPao(nums);
            Console.WriteLine("冒泡排序：");
            for (int i = 0; i < nums.Length; i++)
            {
               Console.Write(nums[i] + " ");
            }
            Console.ReadKey();
        }
        public static T GetMax<T>(T[] items, DelegateCompare<T> del)
        {
            T max = items[0];
            for (var i = 0; i < items.Length; i++)
            {
                if (del(max, items[i]) < 0)
                {
                    max = items[i];
                }
            }
            return max;
        }
        //字符串的比较方法
        public static int Compare2(string n1, string n2)
        {
            return n1.Length - n2.Length;
        }
        
        public static double GetAvg(int[] nums)
        {
            double sum = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                sum += nums[i];
            }
            return sum / nums.Length;
        }

       public static void MaoPao(int[] nums)
       {
            for (int i = 0; i < nums.Length - 1; i++)
            {
                for (int j = 0; j < nums.Length - 1 - i; j++)
                {
                    if (nums[j] > nums[j + 1])
                    {
                        int temp = nums[j];
                        nums[j] = nums[j + 1];
                        nums[j + 1] = temp;
                    }
                }
            }
       }
    }
}

